package com.wang.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.exception.BusinessException;
import com.wang.param.PostCollectionQueryParam;
import com.wang.reponse.PostVo;
import com.wang.user.model.Post;
import com.wang.user.model.PostCollection;
import com.wang.user.model.Upvote;
import com.wang.user.model.User;
import com.wang.user.service.PostCollectionService;
import com.wang.user.mapper.PostCollectionMapper;
import com.wang.user.service.PostService;
import com.wang.user.service.user.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author bkty0
* @description 针对表【post_collection(帖子收藏表)】的数据库操作Service实现
* @createDate 2025-04-23 10:39:21
*/
@Service
public class PostCollectionServiceImpl extends ServiceImpl<PostCollectionMapper, PostCollection>
    implements PostCollectionService{

    @Resource
    private PostCollectionMapper postCollectionMapper;

    @Resource
    private PostService postService;

    @Resource
    private UserService userService;

    @Override
    public List<PostVo> listPostCollection(PostCollectionQueryParam queryParam) {
        User loginUser = userService.getLoginUser();
        LambdaQueryWrapper<PostCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PostCollection::getUserId,loginUser.getUid());
        List<PostCollection> postCollections = list(wrapper);
        if(CollUtil.isEmpty(postCollections)){
            return Collections.emptyList();
        }
        List<Long> postIds = postCollections.stream().distinct().map(PostCollection::getPostId).collect(Collectors.toList());

        return postService.listVoByPostIds(postIds);
    }

    @Override
    public Boolean collection(PostCollection postCollection) {
        Post post = postService.getById(postCollection.getPostId());
        if(post == null){
            throw new BusinessException("帖子数据不存在");
        }
        User loginUser = userService.getLoginUser();
        PostCollection postCollection2 = getByPostIdAndUserId(post.getId(), loginUser.getUid());
        if(ObjectUtil.isNotNull(postCollection2)){
            throw new BusinessException("已收藏，请勿重复收藏");
        }
        PostCollection postCollection1 = new PostCollection();
        postCollection1.setPostId(postCollection.getPostId());
        postCollection1.setUserId(loginUser.getUid());
        return save(postCollection1);
    }

    @Override
    public Boolean cancel(PostCollection postCollection) {
        Post post = postService.getById(postCollection.getPostId());
        if(post == null){
            throw new BusinessException("帖子数据不存在");
        }
        User loginUser = userService.getLoginUser();
        LambdaQueryWrapper<PostCollection> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PostCollection::getPostId,postCollection.getPostId());
        lambdaQueryWrapper.eq(PostCollection::getUserId,loginUser.getUid());
        return remove(lambdaQueryWrapper);
    }

    @Override
    public PostCollection getByPostIdAndUserId(Long id, Integer uid) {
        LambdaQueryWrapper<PostCollection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PostCollection::getPostId,id);
        queryWrapper.eq(PostCollection::getUserId,uid);
        return getOne(queryWrapper);
    }

    @Override
    public List<PostVo> listByUid(Integer uid) {
        LambdaQueryWrapper<PostCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PostCollection::getUserId,uid);
        List<PostCollection> postCollections = list(wrapper);
        if(CollUtil.isEmpty(postCollections)){
            return Collections.emptyList();
        }
        List<Long> postIds = postCollections.stream().distinct().map(PostCollection::getPostId).collect(Collectors.toList());
        return postService.listVoByPostIdsAndUid(postIds,uid);
    }
}




